﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Diploma.Models.IRepositories;
using Diploma.Utils;

namespace Diploma.Models.Repositories
{
    internal class PublicMessagesRepository : IPublicMessagesRepository
    {
        private readonly DiplomaEntities dataContext;

        public PublicMessagesRepository(DiplomaEntities dataContext)
        {
            this.dataContext = dataContext;
        }

        public PublicMessage GetPublicMessage(Guid id)
        {
            return
                dataContext.PublicMessages.Where(m => m.PublicMessageId == id && m.DeletionDate == null).FirstOrDefault
                    ();
        }

        public void Delete(PublicMessage message)
        {
            message.DeletionDate = DateTime.Now;
            dataContext.SaveChanges();
        }

        public IEnumerable<PublicMessage> GetAllPublicMessages()
        {
            return dataContext.PublicMessages.Where(pm => pm.DeletionDate == null).OrderByDescending(m => m.PubDate);
        }

        public IEnumerable<PublicMessage> GetPublicMessages(Guid? startMessageId)
        {
            IEnumerable<PublicMessage> messages =
                dataContext.PublicMessages.Where(pm => pm.DeletionDate == null).OrderByDescending(m => m.PubDate);
            if (messages.LastOrDefault() != null && messages.Last().PublicMessageId == startMessageId)
                return null;
            if (startMessageId.HasValue)
            {
                messages = messages.AsEnumerable().SkipWhile((m, i) => m.PublicMessageId != startMessageId).Skip(1);
            }
            if (messages.Count() > ConstantsUtil.PublicMessagesPortion)
            {
                return messages.Take(ConstantsUtil.PublicMessagesPortion);
            }
            return messages;
        }

        public void Add(PublicMessage message, HttpPostedFileBase file)
        {
            message.PublicMessageId = Guid.NewGuid();
            message.PubDate = DateTime.Now;

            string fullPathToFileAbsolute = null;

            if (file != null)
            {
                fullPathToFileAbsolute = ConstantsUtil.GetPublicFilesDirectoryPath(PathType.Absolute) + file.FileName;
                string fullPathToFileVirual = ConstantsUtil.GetPublicFilesDirectoryPath(PathType.Virtual) + file.FileName;

                if (File.Exists(fullPathToFileAbsolute))
                {
                    fullPathToFileAbsolute = string.Format("{0}{1}_{2}",
                                                           ConstantsUtil.GetPublicFilesDirectoryPath(PathType.Absolute),
                                                           DateTime.Now.ToString("MMddyyyyHmmss"), file.FileName);
                    fullPathToFileVirual = string.Format("{0}{1}_{2}",
                                                         ConstantsUtil.GetPublicFilesDirectoryPath(PathType.Virtual),
                                                         DateTime.Now.ToString("MMddyyyyHmmss"), file.FileName);
                }

                file.SaveAs(fullPathToFileAbsolute);
                message.AttachedFilePath = fullPathToFileVirual;
            }
            dataContext.AddToPublicMessages(message);
            try
            {
                dataContext.SaveChanges();
            }
            catch (Exception)
            {
                if (fullPathToFileAbsolute != null)
                {
                    File.Delete(fullPathToFileAbsolute);
                    return;
                }
            }
        }
    }
}